﻿using System.Data.SQLite;

namespace RobosimAPI.Class
{
	public class ThemeRule
	{
		/*
		themeRuleList
		id       INTEGER,
		title    TEXT,
		version  TEXT,
		theme_id INTEGER
		cover        TEXT,
		introduction TEXT,
		map_cover    TEXT,
		rule_file    TEXT
		*/
		public int id;
		public string? title;
		public string? version;
		public int theme_id;
		public string? cover;
		public string? introduction;
		public string? map_cover;
		public string? rule_file;
		public ThemeRule(int id)
		{
			string sql = "SELECT * FROM themeRuleList WHERE id = @id LIMIT 1";
			using (var conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
			{
				conn.Open();
				var cmd = new SQLiteCommand(sql, conn);
				cmd.Parameters.AddWithValue("@id", id);
				var reader = cmd.ExecuteReader();
				if (reader.Read())
				{
					this.id = reader.GetInt32(0);
					this.title = reader.GetString(1);
					this.version = reader.GetString(2);
					this.theme_id = reader.GetInt32(3);
					this.cover = reader.IsDBNull(4) ? null : reader.GetString(4);
					this.introduction = reader.IsDBNull(5) ? null : reader.GetString(5);
					this.map_cover = reader.IsDBNull(6) ? null : reader.GetString(6);
					this.rule_file = reader.IsDBNull(7) ? null : reader.GetString(7);
				}
				conn.Close();
				conn.Dispose();
			}
		}
		public ThemeRule() { }
		public static ThemeRule[] GetThemeRulesWhereThemeId(int theme_id)
		{
			List<ThemeRule> themeRules = new List<ThemeRule>();
			using (var conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
			{
				conn.Open();
				var cmd = new SQLiteCommand("SELECT * FROM themeRuleList WHERE theme_id = @theme_id", conn);
				cmd.Parameters.AddWithValue("@theme_id", theme_id);
				var reader = cmd.ExecuteReader();
				while (reader.Read())
				{
					var themeRule = new ThemeRule
					{
						id = reader.GetInt32(0),
						title = reader.GetString(1),
						version = reader.GetString(2),
						theme_id = reader.GetInt32(3),
						cover = reader.IsDBNull(4)? null : reader.GetString(4),
						introduction = reader.IsDBNull(5)? null : reader.GetString(5),
						map_cover = reader.IsDBNull(6)? null : reader.GetString(6),
						rule_file = reader.IsDBNull(7)? null : reader.GetString(7)
					};
					themeRules.Add(themeRule);
				}
				conn.Close();
				conn.Dispose();
			}
			return themeRules.ToArray();
		}
		public static ThemeRule[] GetAllThemeRules()
		{
			List<ThemeRule> themeRules = new List<ThemeRule>();
			using (var conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
			{
				conn.Open();
				var cmd = new SQLiteCommand("SELECT * FROM themeRuleList", conn);
				var reader = cmd.ExecuteReader();
				while (reader.Read())
				{
					var themeRule = new ThemeRule
					{
						id = reader.GetInt32(0),
						title = reader.GetString(1),
						version = reader.GetString(2),
						theme_id = reader.GetInt32(3),
						cover = reader.IsDBNull(4) ? null : reader.GetString(4),
						introduction = reader.IsDBNull(5) ? null : reader.GetString(5),
						map_cover = reader.IsDBNull(6) ? null : reader.GetString(6),
						rule_file = reader.IsDBNull(7)? null : reader.GetString(7)
					};
					themeRules.Add(themeRule);
				}
				conn.Close();
				conn.Dispose();
			}
			return themeRules.ToArray();
		}
		public void UpdateAll()
		{
			using (var conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
			{
				conn.Open();
				var cmd = new SQLiteCommand("UPDATE themeRuleList SET title = @title, version = @version, theme_id = @theme_id, cover = @cover, introduction = @introduction, map_cover = @map_cover, rule_file = @rule_file WHERE id = @id", conn);
				cmd.Parameters.AddWithValue("@id", id);
				cmd.Parameters.AddWithValue("@title", title);
				cmd.Parameters.AddWithValue("@version", version);
				cmd.Parameters.AddWithValue("@theme_id", theme_id);
				cmd.Parameters.AddWithValue("@cover", cover);
				cmd.Parameters.AddWithValue("@introduction", introduction);
				cmd.Parameters.AddWithValue("@map_cover", map_cover);
				cmd.Parameters.AddWithValue("@rule_file", rule_file);
				cmd.ExecuteNonQuery();
				conn.Close();
				conn.Dispose();
			}
		}
	}
}
